package service

import (
	"context"
	"errors"
	"github.com/google/uuid"
	__ "server/basic/proto"
	"server/handler/models"
	"server/pkg"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedGoodsServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Register(_ context.Context, in *__.RegisterReq) (*__.RegisterResp, error) {
	user := models.User{
		Username: in.Username,
		Password: pkg.Md5(in.Password),
		Mobile:   in.Mobile,
		Address:  in.Address,
		Realname: in.Realname,
	}
	if err := user.CreateUser(); err != nil {
		return nil, errors.New("用户注册失败")
	}
	return &__.RegisterResp{
		Id: int64(user.Id),
	}, nil
}

// 商品详情
func (s *Server) GetGoodsDetail(_ context.Context, in *__.GetGoodsDetailReq) (*__.GetGoodsDetailResp, error) {
	var goods models.Goods
	goods.Id = uint(in.Id)
	if err := goods.GetGoodsDetail(); err != nil {
		return nil, errors.New("商品详情获取失败")
	}
	return &__.GetGoodsDetailResp{
		Id:          int64(goods.Id),
		GoodsName:   goods.GoodsName,
		GoodsDesc:   goods.GoodsDesc,
		GoodsPrice:  int64(goods.GoodsPrice),
		GoodsWeight: int64(goods.GoodsWeight),
		GoodsImage:  goods.GoodsImage,
		GoodsStock:  int64(goods.GoodsStock),
	}, nil
}

// 商品列表
func (s *Server) GetGoodsList(_ context.Context, in *__.GetGoodsListReq) (*__.GetGoodsListResp, error) {
	var goodsLists []*__.GoodsList
	var gl models.Goods
	list, err := gl.GetGoodsList()
	if err != nil {
		return nil, errors.New("收藏列表获取失败")
	}
	for _, goods := range list {
		goodsLists = append(goodsLists, &__.GoodsList{
			Id:          int64(goods.Id),
			GoodsName:   goods.GoodsName,
			GoodsDesc:   goods.GoodsDesc,
			GoodsPrice:  int64(goods.GoodsPrice),
			GoodsWeight: int64(goods.GoodsWeight),
			GoodsImage:  goods.GoodsImage,
			GoodsStock:  int64(goods.GoodsStock),
		})
	}
	return &__.GetGoodsListResp{
		List: goodsLists,
	}, nil
}

// 添加收藏
func (s *Server) CreateCollect(_ context.Context, in *__.CreateCollectReq) (*__.CreateCollectResp, error) {
	collect := models.Collect{
		UserId:     uint(in.UserId),
		GoodsId:    uint(in.GoodsId),
		GoodsPrice: uint(in.GoodsPrice),
		GoodsImage: in.GoodsImage,
	}
	if err := collect.CreateCollect(); err != nil {
		return nil, errors.New("添加收藏失败")
	}
	return &__.CreateCollectResp{
		Id: int64(collect.Id),
	}, nil
}

// 移除收藏
func (s *Server) DeleteCollect(_ context.Context, in *__.DeleteCollectReq) (*__.DeleteCollectResp, error) {
	var collect models.Collect
	collect.Id = uint(in.Id)
	if err := collect.DeleteCollect(); err != nil {
		return nil, errors.New("取消收藏失败")
	}
	return &__.DeleteCollectResp{}, nil
}

// 生成订单
func (s *Server) CreateOrder(_ context.Context, in *__.CreateOrderReq) (*__.CreateOrderResp, error) {
	order := models.Order{
		OrderSn:     uuid.NewString(),
		GoodsId:     uint(in.GoodsId),
		GoodsPrice:  uint(in.GoodsPrice),
		GoodsNum:    uint(in.GoodsNum),
		GoodsMoney:  uint(in.GoodsPrice * in.GoodsNum),
		OrderStatus: 1,
	}
	if err := order.CreateOrder(); err != nil {
		return nil, errors.New("订单生成失败")
	}
	return &__.CreateOrderResp{
		Id: int64(order.Id),
	}, nil
}

// 修改订单
func (s *Server) UpdateOrder(_ context.Context, in *__.UpdateOrderReq) (*__.UpdateOrderResp, error) {
	var order models.Order
	order.Id = uint(in.Id)
	order.OrderStatus = uint(in.OrderStatus)
	if err := order.UpdateOrder(); err != nil {
		return nil, errors.New("订单修改失败")
	}
	return &__.UpdateOrderResp{
		Id: int64(order.Id),
	}, nil
}
